package com.niji.littly_donkey_android.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.makeramen.roundedimageview.RoundedImageView;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.adapter.OrderAdapter;
import com.niji.littly_donkey_android.bean.Order;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.*;
import com.niji.littly_donkey_android.view.CircleImageView;
import org.json.JSONException;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyInfoActivity extends BaseActivity {

    private final static String TAG = "MyInfoActivity";


    private Button my_info_return;

    private Button my_info_setting;

    private Button my_info_paied;

    private Button my_info_checked;

    private Button my_info_commented;

    private Button my_info_refund;

    private Button my_info_checkeded;

    private Button my_info_select_all_order_btn;

    private TextView my_info_select_all_order;

    private TextView my_info_video;

    private Button my_info_video_btn;

    private Button my_info_withdrawal;

    private RelativeLayout my_info_withdrawal_details;

    private RelativeLayout my_info_details;

    private RelativeLayout my_info_set;

    private Button my_info_message_center;

    private TextView my_info_setting_name;

    private CircleImageView my_info_head;

    private TextView my_info_followNumber;

    private TextView my_info_fansNumber;

    private TextView my_info_likesNumber;

    private TextView my_info_videosNumber;

    private TextView my_info_userId;

    private TextView yesterday_revenue;

    private TextView yesterday_withdraw;

    private TextView my_info_shareApp;



    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_info);
        myInfoReturn();
        myInfoSetting();
        myInfoSelectAllOrder();
        myInfoSelectAllOrderBtn();
        MyInfoVideo();
        MyInfoVideoBtn();
        myInfoWithdrawal();
        myInfoWithdrawalDetails();
        myInfoDetails();
        myInfoSet();
        myInfoMessageCenter();
        myInfoPaied();
        myInfoChecked();
        myInfoCommented();
        myInfoRefund();
        myInfoCheckeded();
        myInfoSettingName();
        showYesterdayNumber();
        myInfoHeadUpdate();
        shareApp();
        initPhotoError();
        init();
    }

    /**
     * 通过点击头像修改资料
     */
    private void myInfoHeadUpdate() {
        my_info_head = findViewById(R.id.my_info_head);
        my_info_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyInfoActivity.this, UpdateInfoActivity.class));
            }
        });
    }

    /**
     * 分享APP
     */
    private void shareApp() {
        my_info_shareApp = findViewById(R.id.my_info_shareApp);
        my_info_shareApp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareFile(MyInfoActivity.this);
            }
        });
    }


    /**
     * 分享APP
     * @param context
     */
    public static void shareFile(Context context) {
        File apkFile = AppUtils.getApkFile(context);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("*/*");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(apkFile));
        context.startActivity(intent);
    }


    private void initPhotoError(){
        // android 7.0系统解决拍照的问题
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
    }

    /**
    *  关闭其他界面之后，刷新界面
     */
    @Override
    protected void onResume() {
        super.onResume();
        showYesterdayNumber();
        init();
    }


    /**
     * 展示昨天总收益和收益提现
     */
    private void showYesterdayNumber() {
        yesterday_revenue = findViewById(R.id.yesterday_revenue);
        yesterday_withdraw = findViewById(R.id.yesterday_withdraw);
        String url = UrlUtil.URL + "/order/total/revenue";
        RequestQueue requestQueue = Volley.newRequestQueue(MyInfoActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object> dataMap = (Map) map.get("data");
                        Log.i(TAG,dataMap.toString());
                        Integer amount = (Integer) dataMap.get("amount");
                        yesterday_revenue.setText("￥ " + amount);
                    } else if(code == -1){
                        Toast.makeText(MyInfoActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(MyInfoActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    }else {
                        String message = (String) map.get("message");
                        Toast.makeText(MyInfoActivity.this, message, Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(MyInfoActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };
        String url1 = UrlUtil.URL + "/order/total/revenue/withdraw";
        StringRequest stringRequest1 = new StringRequest(Request.Method.POST, url1, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object> dataMap = (Map) map.get("data");
                        Log.i(TAG,dataMap.toString());
                        Integer amount = (Integer) dataMap.get("amount");
                        yesterday_withdraw.setText("￥ " + amount);
                    } else if(code == -1){
                        Toast.makeText(MyInfoActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(MyInfoActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    }else {
                        String message = (String) map.get("message");
                        Toast.makeText(MyInfoActivity.this, message, Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(MyInfoActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };

        requestQueue.add(stringRequest);
        requestQueue.add(stringRequest1);

    }

    /**
     * 初始化显示数据
     */
    private void init() {
        showInitMessage();
        showMessageNumber();
    }

    /**
     * 显示未读消息数量
     */
    private void showMessageNumber() {
        TextView my_info_message_number = findViewById(R.id.my_info_message_number);
        String url = UrlUtil.URL + "/user/comments/log/list";
        RequestQueue requestQueue= Volley.newRequestQueue(MyInfoActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object> dataMap = (Map) map.get("data");
                        Log.i(TAG,dataMap.toString());
                        Integer total = (Integer) dataMap.get("total");
                        my_info_message_number.setText("" + total);

                    } else if(code == -1){
                        Toast.makeText(MyInfoActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(MyInfoActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    }else {
                        String message = (String) map.get("message");
                        Toast.makeText(MyInfoActivity.this, message, Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(MyInfoActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };

        requestQueue.add(stringRequest);
    }

    /**
     * 更新数据
     */
    private void showInitMessage() {
        my_info_head = findViewById(R.id.my_info_head);
        my_info_followNumber = findViewById(R.id.my_info_followNumber);
        my_info_fansNumber = findViewById(R.id.my_info_fansNumber);
        my_info_likesNumber = findViewById(R.id.my_info_likesNumber);
        my_info_videosNumber = findViewById(R.id.my_info_videosNumber);
        my_info_userId = findViewById(R.id.my_info_userId);
        String url = UrlUtil.URL + "/user/user/info";
        RequestQueue requestQueue= Volley.newRequestQueue(MyInfoActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object> dataMap = (Map) map.get("data");
                        Log.i(TAG,dataMap.toString());
                        Integer id = (Integer) dataMap.get("id");
                        String nickname = (String) dataMap.get("nickname");
                        String avatar = (String) dataMap.get("avatar");
                        Integer follow_num = (Integer) dataMap.get("follow_num");
                        Integer fans_num = (Integer) dataMap.get("fans_num");
                        Integer likes_num = (Integer) dataMap.get("likes_num");
                        Integer videos_num = (Integer) dataMap.get("videos_num");
                        my_info_userId.setText("" + id);
                        my_info_videosNumber.setText("" + videos_num);
                        my_info_likesNumber.setText("" + likes_num);
                        my_info_fansNumber.setText("" + fans_num);
                        my_info_followNumber.setText("" + follow_num);
                        my_info_setting_name.setText(nickname);
                        String headUrl = UrlUtil.URL + avatar;
                        NetworkGlideImageLoader.getInstance().displayImage((Activity) MyInfoActivity.this,headUrl,my_info_head,0,0);
                        RelativeLayout my_info_loading = findViewById(R.id.my_info_loading);
                        RelativeLayout my_info_main = findViewById(R.id.my_info_main);
                        my_info_main.setVisibility(View.VISIBLE);
                        my_info_loading.setVisibility(View.INVISIBLE);
                    } else if(code == -1){
                        Toast.makeText(MyInfoActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(MyInfoActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(MyInfoActivity.this, "用户信息获取失败", Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(MyInfoActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };

        requestQueue.add(stringRequest);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.my_info;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }

    /**
     * 修改信息事件
     */
    private void myInfoSettingName() {
        my_info_setting_name = findViewById(R.id.my_info_setting_name);
        my_info_setting_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyInfoActivity.this, UpdateInfoActivity.class));
                   
            }
        });
    }

    /**
     * 查看视频
     */
    private void MyInfoVideoBtn() {
        my_info_video_btn = findViewById(R.id.my_info_video_btn);
        my_info_video_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyInfoActivity.this, MyHomePageActivity.class);
                startActivity(intent);
                   
            }
        });
    }

    /**
     * 待支付
     */
    private void myInfoPaied() {
        my_info_paied = findViewById(R.id.my_info_paied);
        my_info_paied.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyInfoActivity.this, MyAllOrderActivity.class);
                intent.putExtra("orderStatus","1");
                startActivity(intent);

                   
            }
        });
    }

    /**
     * 待检票
     */
    private void myInfoChecked() {
        my_info_checked = findViewById(R.id.my_info_checked);
        my_info_checked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyInfoActivity.this, MyAllOrderActivity.class);
                intent.putExtra("orderStatus","2");
                startActivity(intent);
                   
            }
        });

    }

    /**
     * 待评价
     */
    private void myInfoCommented() {
        my_info_commented = findViewById(R.id.my_info_commented);
        my_info_commented.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyInfoActivity.this, MyAllOrderActivity.class);
                intent.putExtra("orderStatus","3");
                startActivity(intent);
                   
            }

        });

    }

    /**
     * 退款
     */
    private void myInfoRefund() {
        my_info_refund = findViewById(R.id.my_info_refund);
        my_info_refund.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyInfoActivity.this, MyAllOrderActivity.class);
                intent.putExtra("orderStatus","4");
                startActivity(intent);
                   
            }
        });
    }

    /**
     * 检票码
     */
    private void myInfoCheckeded() {
        my_info_checkeded = findViewById(R.id.my_info_checkeded);
        my_info_checkeded.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyInfoActivity.this, CheckActivity.class));
                   
            }
        });
    }

    /**
     * 查看全部订单
     */
    private void myInfoSelectAllOrderBtn() {
        my_info_select_all_order_btn = findViewById(R.id.my_info_select_all_order_btn);
        my_info_select_all_order_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyInfoActivity.this, MyAllOrderActivity.class));
                   
            }
        });
    }

    /**
     * 消息中心
     */
    private void myInfoMessageCenter() {
        my_info_message_center = findViewById(R.id.my_info_message_center);
        my_info_message_center.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyInfoActivity.this, MessageCenterActivity.class));
                   
            }
        });
    }

    /***
     * 修改信息
     */
    private void myInfoSet() {
        my_info_set = findViewById(R.id.my_info_set);
        my_info_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyInfoActivity.this, SettingActivity.class));
                   
            }
        });
    }

    /**
     * 收益明细
     */
    private void myInfoDetails() {
        my_info_details = findViewById(R.id.my_info_details);
        my_info_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyInfoActivity.this, BillDetailsActivity.class);
                intent.putExtra("mode","details");
                startActivity(intent);
                   
            }
        });
    }

    /**
     * 提现明细
     */
    private void myInfoWithdrawalDetails() {
        my_info_withdrawal_details = findViewById(R.id.my_info_withdrawal_details);
        my_info_withdrawal_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyInfoActivity.this, BillDetailsActivity.class);
                intent.putExtra("mode","withdrawal");
                startActivity(intent);
                   
            }
        });
    }

    /**
     * 提现明细
     */
    private void myInfoWithdrawal() {
        my_info_withdrawal = findViewById(R.id.my_info_withdrawal);
        my_info_withdrawal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyInfoActivity.this, BillBillActivity.class));
                   
            }
        });
    }


    /**
     * 查看视频
     */
    private void MyInfoVideo() {
        my_info_video = findViewById(R.id.my_info_video);
        my_info_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyInfoActivity.this, MyHomePageActivity.class));
                   
            }
        });
    }

    /**
     * 返回
     */

    private void myInfoReturn() {
        my_info_return = findViewById(R.id.my_info_return);
        my_info_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });
    }

    /**
     * 设置
     */
    private void myInfoSetting() {
        my_info_setting = findViewById(R.id.my_info_setting);
        my_info_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyInfoActivity.this, UpdateInfoActivity.class));
                   
            }
        });
    }

    /**
     * 查看所有订单
     */
    private void myInfoSelectAllOrder() {
        my_info_select_all_order = findViewById(R.id.my_info_select_all_order);
        my_info_select_all_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyInfoActivity.this, MyAllOrderActivity.class));
                   
            }
        });
    }



}
